using UnityEngine;

namespace GameCore
{
    public class PlayerInventory : GridInventory
    {
        [Header("Adventure Inventory Settings")]
        public Vector2Int size = new Vector2Int(10, 10);

        public GameObject bgAdventure;
        public GameObject bgBaseLeft;
        public GameObject bgBaseRight;

        public Transform rootAdventure;
        public Transform rootBaseLeft;
        public Transform rootBaseRight;
        
        private void Awake()
        {
            shape = new Shape();
            for (int x = 0; x < size.x; x++)
            {
                for (int y = 0; y < size.y; y++)
                {
                    shape.AddCell(new Vector2Int(x, y));
                }
            }
        }

        public void SetPositionAdventure()
        {
            bgAdventure.SetActive(true);
            bgBaseLeft.SetActive(false);
            bgBaseRight.SetActive(false);
            cellRoot.transform.position = rootAdventure.transform.position;
        }
        
        public void SetPositionBaseLeft()
        {
            bgBaseLeft.SetActive(false);
            bgBaseLeft.SetActive(true);
            bgBaseRight.SetActive(false);
            cellRoot.transform.position = rootBaseLeft.transform.position;
        }

        public void SetPositionBaseRight()
        {
            bgAdventure.SetActive(false);
            bgBaseLeft.SetActive(false);
            bgBaseRight.SetActive(true);
            cellRoot.transform.position = rootBaseRight.transform.position;
        }

        public (int total, int used) CalculateGrids()
        {
            int totalGrids = data.shape.Size;
            int usedGrids = data.occupation.Size;
            foreach (var item in gridItems.Values)
            {
                if (item is SimpleGridBackpack backpack && backpack.itemData.HasData("inventory"))
                {
                    var inventory = backpack.itemData.GetData("inventory") as InventoryData;
                    totalGrids += inventory.shape.Size;
                    usedGrids += inventory.occupation.Size;
                }
            }
            return (totalGrids, usedGrids);
        }
    }
}